home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / otm3d095 / 3dtools.ref < prev    next >
Encoding:
Text File  |  1994-12-12  |  19.2 KB  |  728 lines

  1.         3dtools v0.95 function reference
  2.  
  3.         Copyright (C) 1994 Zach Mortensen [Voltaire/OTM]
  4.  
  5.  
  6.         This document describes the classes used in 3dtools, along 
  7.         with their member data and functions.  This is NOT a tutorial
  8.         3d math, nor is it a tutorial in object oriented programming.
  9.  
  10.  
  11.  
  12.  
  13. int dotProduct(point3d *p1, point3d *p2);
  14.  
  15.         Returns the dot product of two vectors, p1 and p2.  This function is
  16.         not a member of any class.
  17.  
  18.  
  19.  
  20.  
  21. class point3d
  22.  
  23.         This class is used to store all the data pertinent to 3d points
  24.         (vectors).  Objects of type point3d are the basis for all other
  25.         objects used by 3dtools, so it is vital that the workings of this
  26.         object be understood.
  27.  
  28.  
  29. MEMBER DATA
  30.  
  31. public:
  32.  
  33.     long x3d, y3d, z3d;
  34.  
  35.         x3d, y3d, and z3d are the GLOBAL coordinates of this point.  GLOBAL
  36.         coordinates are defined in terms of THE origin <0,0,0>.  After 
  37.         every operation on the point, a new set of global coordinates is
  38.         calculated which represent the new position of the point with 
  39.         respect to THE (<0,0,0>) origin.
  40.  
  41.  
  42.     long localX, localY, localZ;
  43.  
  44.         localX, localY, and localZ are the LOCAL coordinates of this point.
  45.         LOCAL coordinates are defined in terms of another point, usually
  46.         the center of an OBJECT which contains many points.  The LOCAL 
  47.         coordinates stored in these variables are not modified by any 
  48.         operation.  This is useful if you want to implement a different
  49.         type of rotation which employs the use of unit vectors.  I 
  50.         implemented unit vector rotation, but it turned out to be slightly 
  51.         slower than matrix rotation so I scrapped it.  The original local
  52.         coordinates still come in handy from time to time, so I kept them.
  53.  
  54.  
  55.     long rotoX, rotoY, rotoZ;
  56.  
  57.         rotoX, rotoY, and rotoZ are the ROTATED LOCAL coordinates of this
  58.         point.  They are redefined with every rotational operation.
  59.  
  60.  
  61.     long oX, oY, oZ;
  62.  
  63.         oX, oY, and oZ are the coordinates of the LOCAL ORIGIN which the 
  64.         local coordinates (localX, localY, localZ) are defined in terms of.
  65.         oX, oY, and oZ are defined in terms of THE (<0,0,0>) origin.  There
  66.         are a number of member functions which are designed to perform 
  67.         various operations on the origin.
  68.  
  69.  
  70.     long x2d, y2d;
  71.  
  72.         x2d and y2d are the SCREEN coordinates of this point.  A perspective
  73.         transformation is applied to the GLOBAL coordinates to obtain the
  74.         SCREEN coordinates.
  75.  
  76.  
  77.     long nX, nY, nZ, nCount;
  78.  
  79.         nX, nY, and nZ are the coordinates of a NORMAL VECTOR which is the
  80.         average of the normal vectors of all planes which share this point.
  81.         These variables are used to find the average of the normals only,
  82.         they do NOT contain updated normal coordinates following any 
  83.         operations done on this point.  nCount contains the number of 
  84.         normals currently summed in these variables.
  85.  
  86.  
  87.     void *normal;
  88.  
  89.         normal is a pointer to another point3d object which contains the 
  90.         data for a vector which is the average of the normals of all planes
  91.         that share this point.  It is of type void in order to avoid 
  92.         recursive definitions, in which every normal would have an actual
  93.         class point3d normal which would have its own normal...and we would
  94.         have no more memory!  The (void *) is typecast to (point3d *) when
  95.         the data is needed.  The information contained here is used by
  96.         Gouraud shading routines to determine the average intensity of
  97.         light falling on this point.
  98.  
  99. private:
  100.  
  101.     long i, j, k;
  102.  
  103.         These are temporary integers used in many different calculations,
  104.         including non matrix rotation and vector normalization.  They are
  105.         private because the values contained in them are not very important
  106.         to anyone besides this point.
  107.  
  108.  
  109.     int xDeg, yDeg, zDeg;
  110.  
  111.         xDeg, yDeg, and zDeg store the total number of degrees this object
  112.         has been rotated about each respective axis.  This quantity is
  113.         positive and % 360, so values are always in the range 0-359.
  114.  
  115.  
  116.  
  117.  
  118.         MEMBER FUNCTIONS
  119.  
  120.  
  121.     void save(FILE *fp);
  122.     void load(FILE *fp);
  123.  
  124.         THESE DO NOT WORK, SO DO NOT USE THEM!  They were left over from
  125.         an old version of 3dtools, and they do not save all the necessary
  126.         data to reconstruct the newest objects.  If you feel the urge, you 
  127.         may rewrite them to suit your needs.
  128.  
  129.  
  130.     void setTo(long x, long y, long z);
  131.  
  132.         This function sets the point to GLOBAL AND LOCAL coordinates <x,y,z>.
  133.         it also redifines the point's origin to be THE (<0,0,0>) origin.
  134.  
  135.  
  136.     void xform2d();
  137.  
  138.         Applies a perspective transformation to the point, calculating new
  139.         screen coordinates <x2d, y2d>.  Because some points are not displayed
  140.         (some represent normal vectors), this function is NOT automatically
  141.         called by the point itself following any operations.
  142.  
  143.  
  144.     void display(int color);
  145.  
  146.         Displays this point at screen coordinates <x2d, y2d> in color (color).
  147.  
  148.  
  149.     void setNewOrigin(point3d *p);
  150.  
  151.         Sets the origin coordinates <oX,oY,oZ> to the <x3d,y3d,z3d>
  152.         coordinates of point p.  The local coordinates of this point remain
  153.         the same, and the global coordinates are altered to represent the
  154.         local coordinates in terms of the new origin.
  155.  
  156.  
  157.     void globalXform2origin(point3d *p);
  158.  
  159.         Same as setNewOrigin, with the exception that it performs a global
  160.         transformation on the point's <rotoX,rotoY,rotoZ> coordinates to 
  161.         obtain a new set of global coordinates defined in terms of the 
  162.         new origin's global coordinates.
  163.  
  164.  
  165.     void copyOrigin(point3d *p);
  166.  
  167.         Same as globalXform2origin, with the exception that the global
  168.         transformation is applied using the ORIGIN (<oX,oY,oZ>) coordinates
  169.         of point p rather than the GLOBAL (<x3d,y3d,z3d>) coordinates.
  170.  
  171.  
  172.     void localRotate(int tX, int tY, int tZ);
  173.  
  174.         Rotates this point about its LOCAL (<oX,oY,oZ>) origin.  Adds
  175.         <tX,tY,tZ> to <xDeg,yDeg,zDeg> and calculates the new 
  176.         <rotoX,rotoY,rotoZ> coordinates.
  177.  
  178.  
  179.     void localRotate(int *trig);
  180.  
  181.         This function performs the same task as the other localRotate,
  182.         with the exception that it is used almost exclusively by the obj3d
  183.         class to rotate its member points.  It is much faster than the other
  184.         localRotate function because the trigonometric multipliers which are
  185.         stored in the trig array are constant for all points in a given
  186.         object.  The structure of array trig is as follows
  187.  
  188.         trig[0] = sin tX
  189.         trig[1] = cos tX
  190.         trig[2] = sin tY
  191.         trig[3] = cos tY
  192.         trig[4] = sin tZ
  193.         trig[5] = cos tZ
  194.  
  195.         All these quantities must be expressed in 8.8 fixed point.
  196.  
  197.  
  198.     void matRotate();
  199.  
  200.         Fastest type of local rotation, makes use of a rotation matrix set 
  201.         up by an instance of class obj3d.  Matrix rotation is slower for
  202.         objects of less than 4 points because of the overhead required to
  203.         set up the matrix.
  204.  
  205.  
  206.     void globalRotate(int *trig);
  207.  
  208.         Rotates the point about THE (<0,0,0>) origin.  The trig array in 
  209.         this case is also passed from an instance of class obj3d, however
  210.         the multipliers in the array are constant for every point in the
  211.         3d world.  The trig array has the same format as the array used
  212.         by localRotate(int *trig).
  213.  
  214.  
  215.     void translate(long dX, long dY, long dZ);
  216.  
  217.         Translates (moves) this point.  New coordinates are <x3d + dX,
  218.         y3d + dY, z3d + dZ>.
  219.  
  220.  
  221.     void globalXform();
  222.  
  223.         Performs a global transformation on this point, adding  
  224.         <rotoX,rotoY,rotoZ> to <oX,oY,oZ> to obtain <x3d,y3d,z3d>.
  225.  
  226.  
  227.     void zeroNormal();
  228.  
  229.         Clears the values stored in <nX,nY,nZ> to prepare for calculating
  230.         a new normal value.
  231.  
  232.  
  233.     void addNormal(int dX, int dY, int dZ);
  234.  
  235.         Adds <dX,dY,dZ> to <nX,nY,nZ> and increments nCount.
  236.  
  237.  
  238.     void avgNormal();
  239.  
  240.         Divides <nX,nY,nZ> by nCount to obtain an average normal for this
  241.         point, and adjusts (normalizes) the resultant vector to have a 
  242.         magnitude of 256.
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249. class line3d
  250.  
  251.  
  252.         The line3d class was an integral part of 3dtools at one time, but
  253.         it is fairly useless now considering the fact that I began writing
  254.         my own video library and have absolutely no desire to write a line
  255.         drawing routine.  Its primary function is to display lines for 
  256.         wireframe models, however, it also contains functions dealing with
  257.         a parametric representation of itself.
  258.  
  259.  
  260.         MEMBER DATA
  261.  
  262. private:
  263.  
  264.     point3d *point1, *point2;
  265.  
  266.         The endpoints of the line.
  267.  
  268.  
  269.     int color;
  270.  
  271.         The color used in displaying the line.
  272.  
  273.  
  274.     long i, j, k;
  275.  
  276.         Temporary values used in parametric calculations.
  277.  
  278.  
  279.  
  280.  
  281.         MEMBER FUNCTIONS
  282.  
  283. public:
  284.  
  285.     void draw();
  286.  
  287.         Draws the line using its designated color.
  288.  
  289.  
  290.     void calcVectors();
  291.  
  292.         Calculate vectors used in parametric calculations.
  293.  
  294.  
  295.     long xOfT(double t);
  296.     long yOfT(double t);
  297.     long zOfT(double t);
  298.  
  299.         Return the value of <x,y,z> given a parameter t.
  300.  
  301.  
  302.     double tOfX(long x);
  303.     double tOfY(long y);
  304.     double tOfZ(long z);
  305.  
  306.         Return the value of a parameter t at a given <x,y,z>.
  307.  
  308.  
  309.     void localRotate(double tX, double tY, double tZ);
  310.  
  311.         Calls localRotate for the endpoints of the line, results are
  312.         unpredictable if p1 and p2 are defined in terms of different 
  313.         origins.
  314.  
  315.  
  316.  
  317.  
  318.         Constants for class polygon
  319.  
  320. #define sNone       0
  321. #define sFlat       1
  322. #define sGouraud    2
  323.  
  324.         Used to designate the type of shading to be used when drawing this
  325.         polygon.
  326.  
  327.  
  328. #define fBoth       0
  329. #define fInside     1
  330. #define fOutside    2
  331.  
  332.         Used by plane elimination routines to define which direction the 
  333.         plane is facing when its points are listed in a clockwise manner
  334.         (3dStudio lists its points clockwise, but the mathematically 
  335.         correct order is counterclockwise).
  336.  
  337.  
  338.  
  339. class polygon
  340.  
  341.  
  342.         MEMBER DATA
  343.  
  344. public:
  345.  
  346.     point3d *p1, *p2, *p3, *normal;
  347.  
  348.         p1, p2, and p3 define the vertices of the polygon.  normal defines
  349.         a vector perpendicular to the plane.
  350.  
  351.  
  352.     int color;
  353.  
  354.         The color of this plane.  In flat or gouraud shaded planes, color
  355.         MUST be the first in a range of 16 palette colors ranging from
  356.         darkest to lightest in order for the plane to be shaded correctly!
  357.  
  358.  
  359.     int facing, shading;
  360.  
  361.         The direction this plane is facing, and the type of shading to be
  362.         used.
  363.  
  364. private:
  365.  
  366.     line3d **line;
  367.  
  368.         Array of line3d objects which define the lines used to draw plane as
  369.         a wireframe.  
  370.  
  371.  
  372.     int count, dot, dot1, dot2, dot3;
  373.  
  374.         Temporary variables. count is a counter which is reused.  The dot
  375.         variables are used to hold the values of the dot products of 
  376.         each normal while color intensities are being calculated.
  377.  
  378.  
  379.         MEMBER FUNCTIONS
  380.  
  381.  
  382.     void setTo(point3d *v1, point3d *v2, point3d *v3);
  383.  
  384.         Defines the plane in terms of {v1,v2,v3}.
  385.  
  386.  
  387.     long avgZ();
  388.  
  389.         Returns the average z value of the plane, used in sorting routines.
  390.         The divide in this routine may be eliminated with no loss in sorting
  391.         accuracy.
  392.  
  393.  
  394.     void setColor(int c);
  395.  
  396.         Set this plane to color c, or a range of 16 colors beginning at c.
  397.  
  398.  
  399.     void wireFrame();
  400.  
  401.         Displays the polygon as a wireframe, not currently implemented due
  402.         to laziness on my part.  I don't want to waste time coding a line
  403.         drawing routine, and I never use wireframes, so why bother?
  404.  
  405.  
  406.     void paintSolid();
  407.  
  408.         Flat fills the polygon, no z-buffering.
  409.  
  410.  
  411.     void gShade();
  412.  
  413.         Gouraud shades the polygon, calls gzpoly3 to use z-buffered 
  414.         drawing.  To eliminate z-buffered drawing, call gpoly3 instead.
  415.  
  416.  
  417.     void setNewOrigin(point3d *p);
  418.  
  419.         Calls setNewOrigin for all vertices and the normal.
  420.  
  421.  
  422.     void localRotate(int *trig);
  423.  
  424.         Calls localRotate for all vertices and the normal, not advisable in
  425.         instances of class obj3d due to the fact that multiple planes share
  426.         a single point.
  427.  
  428.  
  429.     void globalRotate(int *trig);
  430.  
  431.         Calls globalRotate for vertices and the normal.  Same word of 
  432.         caution when dealing with class obj3d.
  433.  
  434.  
  435.     void setGNormals();
  436.  
  437.         Adds the calls addNormal(normal) for each of the vertices of this
  438.         plane.
  439.  
  440.  
  441.     void setShading(int shade);
  442.  
  443.         Set the type of shading to be used.
  444.  
  445.  
  446.     void setFacing(int face);
  447.  
  448.         Set the direction in which the plane is facing.
  449.  
  450.  
  451.     void display();
  452.  
  453.         Display the plane using the information contained in (shading) and
  454.         (facing).
  455.  
  456.  
  457.     void zbFlat();
  458.  
  459.         Flat fills the polygon using z-buffered drawing.
  460.  
  461.  
  462.  
  463.  
  464. class obj3d
  465.  
  466.  
  467.         MEMBER DATA
  468.  
  469. private:
  470.  
  471.     point3d *origin;
  472.  
  473.         This point contains information about the local origin of the 
  474.         object.
  475.  
  476.  
  477.     line3d *xAxis, *yAxis, *zAxis;
  478.  
  479.         These were originally to define unit vectors of the object, they 
  480.         have no use now,
  481.  
  482.  
  483.     int *trig;
  484.  
  485.         Contains rotation multipliers which are constant for the entire
  486.         object
  487.  
  488.  
  489. public:
  490.  
  491.     point3d  **point;
  492.  
  493.         List of points which make up the object.
  494.  
  495.  
  496.     point3d  **normal;
  497.  
  498.         List of normals to this object's points and planes.  Points in this
  499.         list are rotated, translated, etc. but are not perspective 
  500.         transformed to obtain screen coordinates since they are never
  501.         displayed.
  502.  
  503.  
  504.     polygon  **poly;
  505.  
  506.         List of polygons which make up the object.
  507.  
  508.  
  509.     int numPoints, numNormals, numPolys;
  510.  
  511.         Number of points, normals, and polygons present in the respective
  512.         lists.
  513.  
  514.  
  515.     int xDeg, yDeg, zDeg;
  516.  
  517.         Number of degrees the object has been rotated about each respective
  518.         axis, always in the range 0-359.
  519.  
  520.  
  521.     int count;
  522.  
  523.         Counter variable.
  524.  
  525.  
  526.  
  527.  
  528.         MEMBER FUNCTIONS
  529.  
  530.  
  531.  
  532.     void save(FILE *fp);
  533.     void load(FILE *fp);
  534.  
  535.         THESE DON'T WORK, they were designed for a previous version and no 
  536.         longer store enough information.  They can be easily modified .
  537.  
  538.     void addLocalPoint(long x, long y, long z);
  539.  
  540.         Add a point, redefining its global coordinates in terms of the 
  541.         object's origin.  The point's local coordinates remain the same
  542.  
  543.  
  544.     void addLocalPoint(point3d *p);
  545.  
  546.         Same as the above.
  547.  
  548.  
  549.     void addGlobalPoint(long x, long y, long z);
  550.  
  551.         Add a point, redefining its local coordinates in terms of the 
  552.         object's origin.  The points global coordinates remain the same.
  553.  
  554.  
  555.     void addGlobalPoint(point3d *p);
  556.  
  557.         Same as the above.
  558.  
  559.  
  560.     void addLocalPoly(polygon *pg);
  561.  
  562.         Add a polygon and adds its normal to the normal list.  Does NOT add
  563.         the vertices to the point list!
  564.  
  565.  
  566.     void addLocalPoly(int p1, int p2, int p3, int c);
  567.  
  568.         Creates a new polygon defined in terms of existing points in the 
  569.         point list and adds it to the object.
  570.  
  571.  
  572.     void addGlobalPoly(polygon *pg);
  573.  
  574.         Adds a polygon whose vertices are defined in terms of THE (<0,0,0>)
  575.         origin.
  576.  
  577.  
  578.     int getPointNum(long x, long y, long z);
  579.  
  580.         Returns the index in the point list of a point with the given LOCAL
  581.         coordinates.  If a point is not found, one is created and added 
  582.         to the list.
  583.  
  584.  
  585.     int getPointNum(point3d *p);
  586.  
  587.         Returns the index in the point list of a point, returns (-1) if
  588.         is not found.
  589.  
  590.  
  591.     void localRotate(int tX, int tY, int tZ);
  592.  
  593.         Rotate the object around its origin.  Rotates points and normals,
  594.         and perspective transforms points only.
  595.  
  596.  
  597.     void globalRotate(int *trig);
  598.  
  599.         Rotate the object about THE (<0,0,0>) origin.  Same methodology 
  600.         as above.
  601.  
  602.  
  603.     void translate(int dX, int dY, int dZ);
  604.  
  605.         Adds <dX,dY,dZ> to the origin, each point and each normal.
  606.  
  607.  
  608.     void sortPlanes();
  609.  
  610.         Uses a linear sort to depth sort the planes from back to front.
  611.  
  612.  
  613.     void paintDots();
  614.  
  615.         Display each point in the point list.
  616.  
  617.  
  618.     void wireFrame();
  619.  
  620.         Display each polygon as a wireframe.
  621.  
  622.  
  623.     void paintSolid();
  624.  
  625.         Flat fill each polygon by calling the paintSolid member of each 
  626.         polygon in the list.
  627.  
  628.  
  629.     void gShade();
  630.  
  631.         Gouraud shade each polygon by calling the gShade member of each
  632.         polygon in the list.
  633.  
  634.  
  635.     void setLocation(long x, long y, long z);
  636.  
  637.         Set the location of this object in terms of THE (<0,0,0>) origin.
  638.  
  639.  
  640.     void setGNormals();
  641.  
  642.         Set up normals to points, necessary for gouraud shading an object.
  643.         However, use only if you are gouraud shading, this function doubles
  644.         the number of normals in the normal list!
  645.  
  646.  
  647.     void display();
  648.  
  649.         Display each polygon by calling the display member of each polygon
  650.         in the list.
  651.  
  652.  
  653.     void zbFlat();
  654.  
  655.         Display each polygon by calling the zbFlat member of each polygon 
  656.         in the list.
  657.  
  658.  
  659.     void addNormal(long x, long y, long z);
  660.  
  661.         Create a normal with the given LOCAL coordinates and add it to the 
  662.         normal list.
  663.  
  664.  
  665.     void addNormal(point3d *p);
  666.  
  667.         Add a normal to the normal list.
  668.  
  669.  
  670.     void matRotate(int tX, int tY, int tZ);
  671.  
  672.         Set up a rotation matrix and call the matRotate member of each
  673.         point and normal, then perspective transforms all points in the 
  674.         point list.  This is by FAR the fastest way to rotate.
  675.  
  676.  
  677.  
  678.         W       A       R       N       I       N       G
  679.  
  680.  
  681.         The world3d and viewPoint classes are not fully implemented!
  682.  
  683. class world3d
  684.  
  685.         Totally unimplemented...nothing useful as of yet
  686.  
  687.  
  688. extern world3d world;
  689.  
  690.         This is THE WORLD, all instances of class obj3d will be listed in
  691.         world, and consequently the world may be rotated, translated, etc.
  692.  
  693.  
  694. class viewPoint
  695.  
  696.  
  697.         MEMBER DATA
  698.  
  699. public:
  700.  
  701.     point3d *location, *light, *view;
  702.  
  703.         location is the location of the eye, which is always <0,0,0>.
  704.         light is a vector of magnitude 1 which defines the direction of
  705.         the light in the world.  A magnitude of 1 combined with an integer
  706.         coordinate system means that the only valid values for light are
  707.         <0,0,1> <0,1,0> <1,0,0> <0,0,-1> <0,-1,0> and <-1,0,0>.  The default
  708.         is <0,0,1>, in which case the light is coming from a point an 
  709.         infinite distance behind the eye and is travelling into the plane of
  710.         the screen.  view defines the current viewing vector, which is always
  711.         <0,0,1>, the eye is always looking directly into the screen.
  712.  
  713.  
  714.         MEMBER FUNCTIONS
  715.  
  716.         none!  None are currently implemented, that is...
  717.  
  718.  
  719. extern viewPoint camera;
  720.  
  721.  
  722.         THE eye, YOU, the viewer...
  723.  
  724.  
  725.  
  726.  
  727.  
  728.